package com.yunji.common.utils;

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author peter-zhu
 * @date : 2024/10/14 02:44
 * @description : TODO
 **/
public class ConvertDataUtil {

    @SafeVarargs
    public static <T> List<T> array2List(T... a) {
        if (a == null || a.length == 0) {
            return Lists.newArrayList();
        }
        return Arrays.asList(a);
    }


    public static <K, V> Map<K, List<V>> groupBy(List<V> list, Function<? super V, ? extends K> keyMapper) {
        return list.stream().collect(Collectors.groupingBy(keyMapper));
    }

    public static <K, V> Map<K, List<V>> getGroupBy(Supplier<List<V>> supplier, Function<? super V, ? extends K> keyMapper) {
        List<V> vs = supplier.get();
        return vs.stream().collect(Collectors.groupingBy(keyMapper));
    }


    public static <K, V> Map<K, V> list2Map(List<V> list, Function<? super V, ? extends K> keyMapper) {
        return list.stream().collect(Collectors.toMap(keyMapper, a -> a));
    }

    public static <V, R> Set<R> list2Set(List<V> list, Function<? super V, ? extends R> mapper) {
        return list.stream().map(mapper).collect(Collectors.toSet());
    }

    public static <X, Y, V> Map<X, Y> list2KeyValueMap(List<V> list, Function<? super V, ? extends X> key, Function<? super V, ? extends Y> value) {
        return list.stream().collect(Collectors.toMap(key, value, (k1, k2) -> k1));
    }


    public static <X, Y, V> Map<X, Y> getKeyValueMap(Supplier<List<V>> supplier, Function<? super V, ? extends X> key, Function<? super V, ? extends Y> value) {
        List<V> vs = supplier.get();
        return list2KeyValueMap(vs, key, value);
    }

    public static <K, V> Map<K, V> getDataMap(Supplier<List<V>> supplier, Function<? super V, ? extends K> keyMapper) {
        List<V> vs = supplier.get();
        return list2Map(vs, keyMapper);
    }

    public static <V, R> Set<R> getDataSet(Supplier<List<V>> supplier, Function<? super V, ? extends R> mapper) {
        List<V> vs = supplier.get();
        return list2Set(vs, mapper);
    }

    public static <T, R> List<R> list2FieldList(List<T> list, Function<? super T, ? extends R> mapper) {
        return list.stream().map(mapper).collect(Collectors.toList());
    }

    public static <T, R> Set<R> set2FieldList(List<T> list, Function<? super T, ? extends R> mapper) {
        return list.stream().map(mapper).collect(Collectors.toSet());
    }

//    public static <T, R> void sortList(boolean reverse, List<T> list, Function<? super T, ? extends R>... mapper) {
//        Stream<T> stream = list.stream();
//        for (Function<? super T,? extends R> function : mapper) {
//        }
//        List<R> sorted = stream.sorted(Comparator.comparing(mapper))
//                .collect(Collectors.toList());
//    }
//
//    public static <T, R> void sortList(List<T> list, Function<? super T, ? extends R>... mapper) {
//    }
}
